home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / iproute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  17.7 KB  |  702 lines

  1. /* @(#) $Header: iproute.c,v 1.10 91/07/16 17:55:24 deyke Exp $ */
  2.  
  3. /* Lower half of IP, consisting of gateway routines
  4.  * Includes routing and options processing code
  5.  *
  6.  * Copyright 1991 Phil Karn, KA9Q
  7.  */
  8. #include "global.h"
  9. #include "mbuf.h"
  10. #include "iface.h"
  11. #include "timer.h"
  12. #include "internet.h"
  13. #include "ip.h"
  14. #include "netuser.h"
  15. #include "icmp.h"
  16. #include "rip.h"
  17. #include "trace.h"
  18. #include "pktdrvr.h"
  19. #include "bootp.h"
  20.  
  21. struct route *Routes[32][HASHMOD];      /* Routing table */
  22. struct route R_default = {              /* Default route entry */
  23.     NULLROUTE, NULLROUTE,
  24.     0,0,0,
  25.     RIP_INFINITY            /* Init metric to infinity */
  26. };
  27.  
  28. int32 Ip_addr;
  29. static struct rt_cache Rt_cache;
  30.  
  31. /* Initialize modulo lookup table used by hash_ip() in pcgen.asm */
  32. void
  33. ipinit()
  34. {
  35.     int i;
  36.  
  37.     for(i=0;i<256;i++)
  38.         Hashtab[i] = i % HASHMOD;
  39. }
  40.  
  41. /* Route an IP datagram. This is the "hopper" through which all IP datagrams,
  42.  * coming or going, must pass.
  43.  *
  44.  * "rxbroadcast" is set to indicate that the packet came in on a subnet
  45.  * broadcast. The router will kick the packet upstairs regardless of the
  46.  * IP destination address.
  47.  */
  48. int
  49. ip_route(i_iface,bp,rxbroadcast)
  50. struct iface *i_iface;  /* Input interface */
  51. struct mbuf *bp;        /* Input packet */
  52. int rxbroadcast;        /* True if packet had link broadcast address */
  53. {
  54.     struct ip ip;                   /* IP header being processed */
  55.     int16 ip_len;                   /* IP header length */
  56.     int16 length;                   /* Length of data portion */
  57.     int32 gateway;                  /* Gateway IP address */
  58.     register struct route *rp;      /* Route table entry */
  59.     struct iface *iface;            /* Output interface, possibly forwarded */
  60.     int16 offset;                   /* Offset into current fragment */
  61.     int16 mf_flag;                  /* Original datagram MF flag */
  62.     int strict = 0;                 /* Strict source routing flag */
  63.     char prec;                      /* Extracted from tos field */
  64.     char del;
  65.     char tput;
  66.     char rel;
  67.     int16 opt_len;          /* Length of current option */
  68.     char *opt;              /* -> beginning of current option */
  69.     int i;
  70.     struct mbuf *tbp;
  71.     int ckgood = 1;
  72.     int pointer;            /* Relative pointer index for sroute/rroute */
  73.  
  74.     if(i_iface != NULLIF){
  75.         ipInReceives++; /* Not locally generated */
  76.         i_iface->iprecvcnt++;
  77.     }
  78.     if(len_p(bp) < IPLEN){
  79.         /* The packet is shorter than a legal IP header */
  80.         ipInHdrErrors++;
  81.         free_p(bp);
  82.         return -1;
  83.     }
  84.     /* Sneak a peek at the IP header's IHL field to find its length */
  85.     ip_len = (bp->data[0] & 0xf) << 2;
  86.     if(ip_len < IPLEN){
  87.         /* The IP header length field is too small */
  88.         ipInHdrErrors++;
  89.         free_p(bp);
  90.         return -1;
  91.     }
  92.     if(cksum(NULLHEADER,bp,ip_len) != 0){
  93.         /* Bad IP header checksum; discard */
  94.         ipInHdrErrors++;
  95.         free_p(bp);
  96.         return -1;
  97.     }
  98.     /* Extract IP header */
  99.     ntohip(&ip,&bp);
  100.  
  101.     if(ip.version != IPVERSION){
  102.         /* We can't handle this version of IP */
  103.         ipInHdrErrors++;
  104.         free_p(bp);
  105.         return -1;
  106.     }
  107.  
  108.     if(i_iface != NULLIF && ismyaddr(ip.source) == NULLIF)
  109.         rt_add(ip.source, 32, 0L, i_iface, 1L, 0x7fffffff / 1000, 0);
  110.  
  111.     /* Trim data segment if necessary. */
  112.     length = ip.length - ip_len;    /* Length of data portion */
  113.     trim_mbuf(&bp,length);
  114.  
  115.     /* If we're running low on memory, return a source quench */
  116.     if(!rxbroadcast && availmem() < Memthresh)
  117.         icmp_output(&ip,bp,ICMP_QUENCH,0,NULLICMP);
  118.  
  119.     /* Process options, if any. Also compute length of secondary IP
  120.      * header in case fragmentation is needed later
  121.      */
  122.     strict = 0;
  123.     for(i=0;i<ip.optlen;i += opt_len){
  124.  
  125.         /* First check for the two special 1-byte options */
  126.         switch(ip.options[i] & OPT_NUMBER){
  127.         case IP_EOL:
  128.             goto no_opt;    /* End of options list, we're done */
  129.         case IP_NOOP:
  130.             opt_len = 1;
  131.             continue;       /* No operation, skip to next option */
  132.         }
  133.         /* Not a 1-byte option, so ensure that there's at least
  134.          * two bytes of option left, that the option length is
  135.          * at least two, and that there's enough space left for
  136.          * the specified option length.
  137.          */
  138.         if(ip.optlen - i < 2
  139.          || ((opt_len = uchar(ip.options[i+1])) < 2)
  140.          || ip.optlen - i < opt_len){
  141.             /* Truncated option, send ICMP and drop packet */
  142.             if(!rxbroadcast){
  143.                 union icmp_args icmp_args;
  144.  
  145.                 icmp_args.pointer = IPLEN + i;
  146.                 icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  147.             }
  148.             free_p(bp);
  149.             return -1;
  150.         }
  151.         opt = &ip.options[i];
  152.  
  153.         switch(opt[0] & OPT_NUMBER){
  154.         case IP_SSROUTE:        /* Strict source route & record route */
  155.             strict = 1;     /* note fall-thru */
  156.         case IP_LSROUTE:        /* Loose source route & record route */
  157.             /* Source routes are ignored unless we're in the
  158.              * destination field
  159.              */
  160.             if(opt_len < 3){
  161.                 /* Option is too short to be a legal sroute.
  162.                  * Send an ICMP message and drop it.
  163.                  */
  164.                 if(!rxbroadcast){
  165.                     union icmp_args icmp_args;
  166.  
  167.                     icmp_args.pointer = IPLEN + i;
  168.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  169.                 }
  170.                 free_p(bp);
  171.                 return -1;
  172.             }
  173.             if(ismyaddr(ip.dest) == NULLIF)
  174.                 break;  /* Skip to next option */
  175.             pointer = uchar(opt[2]);
  176.             if(pointer + 4 > opt_len)
  177.                 break;  /* Route exhausted; it's for us */
  178.  
  179.             /* Put address for next hop into destination field,
  180.              * put our address into the route field, and bump
  181.              * the pointer. We've already ensured enough space.
  182.              */
  183.             ip.dest = get32(&opt[pointer]);
  184.             put32(&opt[pointer],locaddr(ip.dest));
  185.             opt[2] += 4;
  186.             ckgood = 0;
  187.             break;
  188.         case IP_RROUTE: /* Record route */
  189.             if(opt_len < 3){
  190.                 /* Option is too short to be a legal rroute.
  191.                  * Send an ICMP message and drop it.
  192.                  */
  193.                 if(!rxbroadcast){
  194.                     union icmp_args icmp_args;
  195.  
  196.                     icmp_args.pointer = IPLEN + i;
  197.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  198.                 }
  199.                 free_p(bp);
  200.                 return -1;
  201.             }
  202.             pointer = uchar(opt[2]);
  203.             if(pointer + 4 > opt_len){
  204.                 /* Route area exhausted; send an ICMP msg */
  205.                 if(!rxbroadcast){
  206.                     union icmp_args icmp_args;
  207.  
  208.                     icmp_args.pointer = IPLEN + i;
  209.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  210.                 }
  211.                 /* Also drop if odd-sized */
  212.                 if(pointer != opt_len){
  213.                     free_p(bp);
  214.                     return -1;
  215.                 }
  216.             } else {
  217.                 /* Add our address to the route.
  218.                  * We've already ensured there's enough space.
  219.                  */
  220.                 put32(&opt[pointer],locaddr(ip.dest));
  221.                 opt[2] += 4;
  222.                 ckgood = 0;
  223.             }
  224.             break;
  225.         }
  226.     }
  227. no_opt:
  228.  
  229.     /* See if it's a broadcast or addressed to us, and kick it upstairs */
  230.     if(ismyaddr(ip.dest) != NULLIF || rxbroadcast ||
  231.         (WantBootp && bootp_validPacket(&ip, &bp))){
  232. #ifdef  GWONLY
  233.     /* We're only a gateway, we have no host level protocols */
  234.         if(!rxbroadcast)
  235.             icmp_output(&ip,bp,ICMP_DEST_UNREACH,
  236.              ICMP_PROT_UNREACH,NULLICMP);
  237.         ipInUnknownProtos++;
  238.         free_p(bp);
  239. #else
  240.         ip_recv(i_iface,&ip,bp,rxbroadcast);
  241. #endif
  242.         return 0;
  243.     }
  244.     /* Packet is not destined to us. If it originated elsewhere, count
  245.      * it as a forwarded datagram.
  246.      */
  247.     if(i_iface != NULLIF)
  248.         ipForwDatagrams++;
  249.  
  250.     /* Adjust the header checksum to allow for the modified TTL */
  251.     ip.checksum += 0x100;
  252.     if((ip.checksum & 0xff00) == 0)
  253.         ip.checksum++;  /* end-around carry */
  254.  
  255.     /* Decrement TTL and discard if zero. We don't have to check
  256.      * rxbroadcast here because it's already been checked
  257.      */
  258.     if(--ip.ttl == 0){
  259.         /* Send ICMP "Time Exceeded" message */
  260.         icmp_output(&ip,bp,ICMP_TIME_EXCEED,0,NULLICMP);
  261.         ipInHdrErrors++;
  262.         free_p(bp);
  263.         return -1;
  264.     }
  265.     /* Look up target address in routing table */
  266.     if((rp = rt_lookup(ip.dest)) == NULLROUTE){
  267.         /* No route exists, return unreachable message (we already
  268.          * know this can't be a broadcast)
  269.          */
  270.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,NULLICMP);
  271.         free_p(bp);
  272.         ipOutNoRoutes++;
  273.         return -1;
  274.     }
  275.     rp->uses++;
  276.  
  277.     /* Check for output forwarding and divert if necessary */
  278.     iface = rp->iface;
  279.     if(iface->forw != NULLIF)
  280.         iface = iface->forw;
  281.  
  282.     /* Find gateway; zero gateway in routing table means "send direct" */
  283.     if(rp->gateway == 0)
  284.         gateway = ip.dest;
  285.     else
  286.         gateway = rp->gateway;
  287.  
  288.     if(strict && gateway != ip.dest){
  289.         /* Strict source routing requires a direct entry
  290.          * Again, we know this isn't a broadcast
  291.          */
  292.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_ROUTE_FAIL,NULLICMP);
  293.         free_p(bp);
  294.         ipOutNoRoutes++;
  295.         return -1;
  296.     }
  297.     prec = PREC(ip.tos);
  298.     del = ip.tos & DELAY;
  299.     tput = ip.tos & THRUPUT;
  300.     rel = ip.tos & RELIABILITY;
  301.  
  302.     if(ip.length <= iface->mtu){
  303.         /* Datagram smaller than interface MTU; put header
  304.          * back on and send normally.
  305.          */
  306.         if((tbp = htonip(&ip,bp,ckgood)) == NULLBUF){
  307.             free_p(bp);
  308.             return -1;
  309.         }
  310.         iface->ipsndcnt++;
  311.         return (*iface->send)(tbp,iface,gateway,prec,del,tput,rel);
  312.     }
  313.     /* Fragmentation needed */
  314.     if(ip.flags.df){
  315.         /* Don't Fragment set; return ICMP message and drop */
  316.         union icmp_args icmp_args;
  317.  
  318.         icmp_args.mtu = iface->mtu;
  319.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,&icmp_args);
  320.         free_p(bp);
  321.         ipFragFails++;
  322.         return -1;
  323.     }
  324.     /* Create fragments */
  325.     offset = ip.offset;
  326.     mf_flag = ip.flags.mf;          /* Save original MF flag */
  327.     while(length != 0){             /* As long as there's data left */
  328.         int16 fragsize;         /* Size of this fragment's data */
  329.         struct mbuf *f_data;    /* Data portion of fragment */
  330.  
  331.         /* After the first fragment, should remove those
  332.          * options that aren't supposed to be copied on fragmentation
  333.          */
  334.         ip.offset = offset;
  335.         if(length + ip_len <= iface->mtu){
  336.             /* Last fragment; send all that remains */
  337.             fragsize = length;
  338.             ip.flags.mf = mf_flag;  /* Pass original MF flag */
  339.         } else {
  340.             /* More to come, so send multiple of 8 bytes */
  341.             fragsize = (iface->mtu - ip_len) & 0xfff8;
  342.             ip.flags.mf = 1;
  343.         }
  344.         ip.length = fragsize + ip_len;
  345.  
  346.         /* Duplicate the fragment */
  347.         dup_p(&f_data,bp,offset,fragsize);
  348.         if(f_data == NULLBUF){
  349.             free_p(bp);
  350.             ipFragFails++;
  351.             return -1;
  352.         }
  353.         /* Put IP header back on, recomputing checksum */
  354.         if((tbp = htonip(&ip,f_data,0)) == NULLBUF){
  355.             free_p(f_data);
  356.             free_p(bp);
  357.             ipFragFails++;
  358.             return -1;
  359.         }
  360.         /* and ship it out */
  361.         if((*iface->send)(tbp,iface,gateway,prec,del,tput,rel) == -1){
  362.             ipFragFails++;
  363.             free_p(bp);
  364.             return -1;
  365.         }
  366.         iface->ipsndcnt++;
  367.         ipFragCreates++;
  368.         offset += fragsize;
  369.         length -= fragsize;
  370.     }
  371.     ipFragOKs++;
  372.     free_p(bp);
  373.     return 0;
  374. }
  375. int
  376. ip_encap(bp,iface,gateway,prec,del,tput,rel)
  377. struct mbuf *bp;
  378. struct iface *iface;
  379. int32 gateway;
  380. int prec;
  381. int del;
  382. int tput;
  383. int rel;
  384. {
  385.     struct ip ip;
  386.  
  387.     dump(iface,IF_TRACE_OUT,CL_NONE,bp);
  388.     iface->rawsndcnt++;
  389.     iface->lastsent = secclock();
  390.  
  391.     if(gateway == 0L){
  392.         /* Gateway must be specified */
  393.         ntohip(&ip,&bp);
  394.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,NULLICMP);
  395.         free_p(bp);
  396.         ipOutNoRoutes++;
  397.         return -1;
  398.     }
  399.     /* Encapsulate in an IP packet from us to the gateway */
  400.     return ip_send(INADDR_ANY,gateway,IP_PTCL,0,0,bp,0,0,0);
  401. }
  402.  
  403. /* Add an entry to the IP routing table. Returns 0 on success, -1 on failure */
  404. struct route *
  405. rt_add(target,bits,gateway,iface,metric,ttl,private)
  406. int32 target;           /* Target IP address prefix */
  407. unsigned int bits;      /* Size of target address prefix in bits (0-32) */
  408. int32 gateway;          /* Optional gateway to be reached via interface */
  409. struct iface *iface;    /* Interface to which packet is to be routed */
  410. int32 metric;           /* Metric for this route entry */
  411. int32 ttl;              /* Lifetime of this route entry in sec */
  412. char private;           /* Inhibit advertising this entry ? */
  413. {
  414.     struct route *rp,**hp;
  415.     struct route *rptmp;
  416.     int32 gwtmp;
  417.  
  418.     if(iface == NULLIF)
  419.         return NULLROUTE;
  420.  
  421.     /* Mask off target according to width */   /* Fix by Tim Shepard */
  422.     target &= ~0L << (32-bits);                /* Fix by Tim Shepard */
  423.  
  424.     if(bits == 32 && ismyaddr(target))
  425.         return NULLROUTE;       /* Don't accept routes to ourselves */
  426.  
  427.     /* Encapsulated routes must specify gateway, and it can't be
  428.      *  ourselves
  429.      */
  430.     if(iface == &Encap && (gateway == 0 || ismyaddr(gateway)))
  431.         return NULLROUTE;
  432.  
  433.     Rt_cache.route = NULLROUTE;     /* Flush cache */
  434.  
  435.     /* Zero bits refers to the default route */
  436.     if(bits == 0){
  437.         rp = &R_default;
  438.     } else {
  439.         rp = rt_blookup(target,bits);
  440.     }
  441.     if(rp == NULLROUTE){
  442.         /* The target is not already in the table, so create a new
  443.          * entry and put it in.
  444.          */
  445.         rp = (struct route *)callocw(1,sizeof(struct route));
  446.         /* Insert at head of table */
  447.         rp->prev = NULLROUTE;
  448.         hp = &Routes[bits-1][hash_ip(target)];
  449.         rp->next = *hp;
  450.         if(rp->next != NULLROUTE)
  451.             rp->next->prev = rp;
  452.         *hp = rp;
  453.         rp->uses = 0;
  454.     }
  455.     rp->target = target;
  456.     rp->bits = bits;
  457.     rp->gateway = gateway;
  458.     rp->metric = metric;
  459.     rp->iface = iface;
  460.     rp->flags = private ? RTPRIVATE : 0;    /* Should anyone be told of this route? */
  461.     rp->timer.func = rt_timeout;  /* Set the timer field */
  462.     rp->timer.arg = (void *)rp;
  463.     set_timer(&rp->timer,ttl*1000L);
  464.     stop_timer(&rp->timer);
  465.     start_timer(&rp->timer); /* start the timer if appropriate */
  466.  
  467.     /* Check to see if this created an encapsulation loop */
  468.     gwtmp = gateway;
  469.     for(;;){
  470.         rptmp = rt_lookup(gwtmp);
  471.         if(rptmp == NULLROUTE)
  472.             break;  /* No route to gateway, so no loop */
  473.         if(rptmp->iface != &Encap)
  474.             break;  /* Non-encap interface, so no loop */
  475.         if(rptmp == rp){
  476.             rt_drop(target,bits);   /* Definite loop */
  477.             return NULLROUTE;
  478.         }
  479.         if(rptmp->gateway != 0)
  480.             gwtmp = rptmp->gateway;
  481.     }
  482.     route_savefile();
  483.     return rp;
  484. }
  485.  
  486. /* Remove an entry from the IP routing table. Returns 0 on success, -1
  487.  * if entry was not in table.
  488.  */
  489. int
  490. rt_drop(target,bits)
  491. int32 target;
  492. unsigned int bits;
  493. {
  494.     register struct route *rp;
  495.  
  496.     Rt_cache.route = NULLROUTE;     /* Flush the cache */
  497.  
  498.     if(bits == 0){
  499.         /* Nail the default entry */
  500.         stop_timer(&R_default.timer);
  501.         R_default.iface = NULLIF;
  502.         return 0;
  503.     }
  504.     if(bits > 32)
  505.         bits = 32;
  506.  
  507.     /* Mask off target according to width */
  508.     target &= ~0L << (32-bits);
  509.  
  510.     /* Search appropriate chain for existing entry */
  511.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  512.         if(rp->target == target)
  513.             break;
  514.     }
  515.     if(rp == NULLROUTE)
  516.         return -1;      /* Not in table */
  517.  
  518.     stop_timer(&rp->timer);
  519.     if(rp->next != NULLROUTE)
  520.         rp->next->prev = rp->prev;
  521.     if(rp->prev != NULLROUTE)
  522.         rp->prev->next = rp->next;
  523.     else
  524.         Routes[bits-1][hash_ip(target)] = rp->next;
  525.  
  526.     free((char *)rp);
  527.     return 0;
  528. }
  529. #if 1
  530. char Hashtab[256];      /* Modulus lookup table */
  531.  
  532. /* Compute hash function on IP address */
  533. int16
  534. hash_ip(addr)
  535. register int32 addr;
  536. {
  537.     register int16 ret;
  538.  
  539.     ret = hiword(addr);
  540.     ret ^= loword(addr);
  541.     return (int16)(ret % HASHMOD);
  542. }
  543. #endif
  544. #ifndef GWONLY
  545. /* Given an IP address, return the MTU of the local interface used to
  546.  * reach that destination. This is used by TCP to avoid local fragmentation
  547.  */
  548. int16
  549. ip_mtu(addr)
  550. int32 addr;
  551. {
  552.     register struct route *rp;
  553.     struct iface *iface;
  554.  
  555.     rp = rt_lookup(addr);
  556.     if(rp == NULLROUTE || rp->iface == NULLIF)
  557.         return 0;
  558.  
  559.     iface = rp->iface;
  560.     if(iface->forw != NULLIF)
  561.         return iface->forw->mtu;
  562.     else
  563.         return iface->mtu;
  564. }
  565. /* Given a destination address, return the IP address of the local
  566.  * interface that will be used to reach it. If there is no route
  567.  * to the destination, pick the first non-loopback address.
  568.  */
  569. int32
  570. locaddr(addr)
  571. int32 addr;
  572. {
  573.     register struct route *rp;
  574.     struct iface *ifp;
  575.  
  576.     if(ismyaddr(addr) != NULLIF)
  577.         return addr;    /* Loopback case */
  578.  
  579.     rp = rt_lookup(addr);
  580.     if(rp != NULLROUTE && rp->iface != NULLIF)
  581.         ifp = rp->iface;
  582.     else {
  583.         /* No route currently exists, so just pick the first real
  584.          * interface and use its address
  585.          */
  586.         for(ifp = Ifaces;ifp != NULLIF;ifp = ifp->next){
  587.             if(ifp != &Loopback && ifp != &Encap)
  588.                 break;
  589.         }
  590.     }
  591.     if(ifp == NULLIF || ifp == &Loopback)
  592.         return 0;       /* No dice */
  593.  
  594.     if(ifp == &Encap){
  595.         /* Recursive call - we assume that there are no circular
  596.          * encapsulation references in the routing table!!
  597.          * (There is a check at the end of rt_add() that goes to
  598.          * great pains to ensure this.)
  599.          */
  600.         return locaddr(rp->gateway);
  601.     }
  602.     if(ifp->forw != NULLIF)
  603.         return ifp->forw->addr;
  604.     else
  605.         return ifp->addr;
  606. }
  607. #endif
  608. /* Look up target in hash table, matching the entry having the largest number
  609.  * of leading bits in common. Return default route if not found;
  610.  * if default route not set, return NULLROUTE
  611.  */
  612. struct route *
  613. rt_lookup(target)
  614. int32 target;
  615. {
  616.     register struct route *rp;
  617.     int bits;
  618.     int32 tsave;
  619.     int32 mask;
  620.  
  621.     /* Examine cache first */
  622.     if(target == Rt_cache.target && Rt_cache.route != NULLROUTE)
  623.         return Rt_cache.route;
  624.  
  625.     tsave = target;
  626.  
  627.     mask = ~0;      /* All ones */
  628.     for(bits = 31;bits >= 0; bits--){
  629.         target &= mask;
  630.         for(rp = Routes[bits][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  631.             if(rp->target == target){
  632.                 /* Stash in cache and return */
  633.                 Rt_cache.target = tsave;
  634.                 Rt_cache.route = rp;
  635.                 return rp;
  636.             }
  637.         }
  638.         mask <<= 1;
  639.     }
  640.     if(R_default.iface != NULLIF){
  641.         Rt_cache.target = tsave;
  642.         Rt_cache.route = &R_default;
  643.         return &R_default;
  644.     } else
  645.         return NULLROUTE;
  646. }
  647. /* Search routing table for entry with specific width */
  648. struct route *
  649. rt_blookup(target,bits)
  650. int32 target;
  651. unsigned int bits;
  652. {
  653.     register struct route *rp;
  654.  
  655.     if(bits == 0){
  656.         if(R_default.iface != NULLIF)
  657.             return &R_default;
  658.         else
  659.             return NULLROUTE;
  660.     }
  661.     /* Mask off target according to width */
  662.     target &= ~0L << (32-bits);
  663.  
  664.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  665.         if(rp->target == target){
  666.             return rp;
  667.         }
  668.     }
  669.     return NULLROUTE;
  670. }
  671. /* Scan the routing table. For each entry, see if there's a less-specific
  672.  * one that points to the same interface and gateway. If so, delete
  673.  * the more specific entry, since it is redundant.
  674.  */
  675. void
  676. rt_merge(trace)
  677. int trace;
  678. {
  679.     int bits,i,j;
  680.     struct route *rp,*rpnext,*rp1;
  681.  
  682.     for(bits=32;bits>0;bits--){
  683.         for(i = 0;i<HASHMOD;i++){
  684.             for(rp = Routes[bits-1][i];rp != NULLROUTE;rp = rpnext){
  685.                 rpnext = rp->next;
  686.                 for(j=bits-1;j >= 0;j--){
  687.                     if((rp1 = rt_blookup(rp->target,j)) != NULLROUTE
  688.                      && rp1->iface == rp->iface
  689.                      && rp1->gateway == rp->gateway){
  690.                         if(trace > 1)
  691.                             printf("merge %s %d\n",
  692.                              inet_ntoa(rp->target),
  693.                              rp->bits);
  694.                         rt_drop(rp->target,rp->bits);
  695.                         break;
  696.                     }
  697.                 }
  698.             }
  699.         }
  700.     }
  701. }
  702.